/**
 * output package name
 */
package com.kingdee.eas.crm.market.client;

import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;

import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.tree.TreeNode;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.ctrl.kdf.table.BasicView;
import com.kingdee.bos.ctrl.kdf.table.ICell;
import com.kingdee.bos.ctrl.kdf.table.IRow;
import com.kingdee.bos.ctrl.kdf.table.KDTable;
import com.kingdee.bos.ctrl.kdf.table.event.KDTMouseEvent;
import com.kingdee.bos.ctrl.kdf.table.event.KDTSelectEvent;
import com.kingdee.bos.ctrl.swing.tree.DefaultKingdeeTreeNode;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.dao.query.IQueryExecutor;
import com.kingdee.bos.metadata.IMetaDataPK;
import com.kingdee.bos.metadata.data.SortType;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.entity.SorterItemCollection;
import com.kingdee.bos.metadata.entity.SorterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.ui.face.CoreUIObject;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.org.OrgStructureInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.common.client.UIContext;
import com.kingdee.eas.common.client.UIFactoryName;
import com.kingdee.eas.crm.market.MarketPlanEntryCollection;
import com.kingdee.eas.crm.market.MarketPlanEntryInfo;
import com.kingdee.eas.crm.market.MarketPlanEntrySellPlanEntryCollection;
import com.kingdee.eas.crm.market.MarketPlanEntrySellPlanEntryInfo;
import com.kingdee.eas.crm.market.MarketPlanFactory;
import com.kingdee.eas.crm.market.MarketPlanInfo;
import com.kingdee.eas.fdc.basecrm.CRMHelper;
import com.kingdee.eas.fdc.basedata.FDCHelper;
import com.kingdee.eas.fdc.basedata.IFDCBill;
import com.kingdee.eas.fdc.basedata.MoneySysTypeEnum;
import com.kingdee.eas.fdc.basedata.client.FDCClientUtils;
import com.kingdee.eas.fdc.basedata.util.KDDetailedAreaDialog;
import com.kingdee.eas.fdc.market.client.MeasurePlanTargetListUI;
import com.kingdee.eas.fdc.sellhouse.SellProjectInfo;
import com.kingdee.eas.fdc.sellhouse.client.FDCTreeHelper;
import com.kingdee.eas.framework.ICoreBase;
import com.kingdee.eas.util.SysUtil;
import com.kingdee.eas.util.client.EASResource;
import com.kingdee.eas.util.client.MsgBox;

/**
 * output class name
 */
public class MarketPlanListUI extends AbstractMarketPlanListUI
{
    private static final Logger logger = CoreUIObject.getLogger(MarketPlanListUI.class);
    
    /**
     * output class constructor
     */
    public MarketPlanListUI() throws Exception
    {
        super();
    }

    protected String getEditUIModal()
    {
/* 101*/        return UIFactoryName.NEWTAB;
    }

    protected String[] getLocateNames()
    {
/* 105*/        String locateNames[] = new String[3];
/* 106*/        locateNames[0] = "number";
/* 107*/        locateNames[1] = "name";
/* 108*/        locateNames[2] = "version";
/* 109*/        return locateNames;
    }

    public void onLoad()
        throws Exception
    {
/* 115*/        actionQuery.setEnabled(false);
/* 116*/        initTree();
/* 117*/        super.onLoad();
/* 118*/        actionQuery.setEnabled(true);
/* 120*/        btnAudit.setIcon(EASResource.getIcon("imgTbtn_audit"));
/* 121*/        btnUnAudit.setIcon(EASResource.getIcon("imgTbtn_unaudit"));
/* 122*/        tblMain.getSelectManager().setSelectMode(2);
/* 124*/        menuItemCancel.setVisible(false);
/* 125*/        menuItemCancelCancel.setVisible(false);
/* 126*/        actionCreateTo.setVisible(false);
/* 127*/        actionCopyTo.setVisible(false);
/* 128*/        actionTraceUp.setVisible(false);
/* 129*/        actionTraceDown.setVisible(false);
/* 130*/        menuBiz.setVisible(false);
/* 132*/        tblMain.getColumn("totalAmount").getStyleAttributes().setHorizontalAlign(com.kingdee.bos.ctrl.kdf.util.style.Styles.HorizontalAlignment.RIGHT);
/* 133*/        tblMain.getColumn("totalAmount").getStyleAttributes().setNumberFormat(FDCHelper.getNumberFtm(2));
/* 135*/        tblMain.getColumn("name").setWidth(220);
    }

    protected String getEditUIName()
    {
    	return com.kingdee.eas.crm.market.client.MarketPlanEditUI.class.getName();
    }

    protected ICoreBase getBizInterface()
        throws Exception
    {
/* 143*/        return MarketPlanFactory.getRemoteInstance();
    }

    protected void treeMain_valueChanged(TreeSelectionEvent e)
        throws Exception
    {
/* 147*/        DefaultKingdeeTreeNode node = (DefaultKingdeeTreeNode)treeMain.getLastSelectedPathComponent();
/* 148*/        if(node == null)
/* 149*/            return;
/* 151*/        if(node.getUserObject() instanceof OrgStructureInfo)
/* 152*/            actionAddNew.setEnabled(false);
/* 154*/        else
/* 154*/            actionAddNew.setEnabled(true);
/* 156*/        refresh(null);
    }

    protected void refresh(ActionEvent e)
        throws Exception
    {
/* 159*/        boolean isSelect = false;
/* 160*/        int rowIndex = tblMain.getSelectManager().getActiveRowIndex();
/* 161*/        if(rowIndex == -1)
/* 162*/            isSelect = true;
/* 164*/        super.refresh(e);
/* 165*/        if(tblMain.getRowCount() == 0)
        {
/* 166*/            tblMain2.removeRows();
/* 167*/            actionAudit.setEnabled(false);
/* 168*/            actionUnAudit.setEnabled(false);
/* 169*/            actionEdit.setEnabled(false);
/* 170*/            actionRemove.setEnabled(false);
        }
/* 172*/        if(isSelect)
/* 173*/            tblMain.getSelectManager().select(0, 0);
    }

    public void actionAudit_actionPerformed(ActionEvent e)
        throws Exception
    {
/* 181*/        java.util.ArrayList idList = getSelectedIdValues();
/* 182*/        ((IFDCBill)getBizInterface()).audit(idList);
/* 183*/        FDCClientUtils.showOprtOK(this);
/* 184*/        refresh(null);
    }

    public void actionUnAudit_actionPerformed(ActionEvent e)
        throws Exception
    {
/* 190*/        java.util.ArrayList idList = getSelectedIdValues();
/* 191*/        ((IFDCBill)getBizInterface()).unAudit(idList);
/* 192*/        FDCClientUtils.showOprtOK(this);
/* 193*/        refresh(null);
    }

    protected void initTree()
        throws Exception
    {
/* 200*/        treeMain.setModel(FDCTreeHelper.getSellProjectTreeForSHE(actionOnLoad, MoneySysTypeEnum.SalehouseSys));
/* 201*/        treeMain.expandAllNodes(true, (TreeNode)treeMain.getModel().getRoot());
/* 202*/        treeMain.setSelectionRow(0);
    }

    public DefaultKingdeeTreeNode getSelectedTreeNode()
    {
/* 206*/        return (DefaultKingdeeTreeNode)treeMain.getLastSelectedPathComponent();
    }

    public SelectorItemCollection getEntrySelectors()
    {
/* 209*/        SelectorItemCollection sel = new SelectorItemCollection();
/* 210*/        sel.add("entry.sellPlanEntry.subject.*");
/* 211*/        sel.add("entry.sellPlanEntry.classify.*");
/* 212*/        sel.add("entry.sellPlanEntry.mediaName.*");
/* 213*/        sel.add("entry.sellPlanEntry.*");
/* 214*/        sel.add("entry.*");
/* 215*/        return sel;
    }

    protected void updateEnterprisePlanEntry()
        throws EASBizException, BOSException
    {
/* 218*/        tblMain2.removeRows();
/* 219*/        int rowIndex = tblMain.getSelectManager().getActiveRowIndex();
/* 220*/        if(rowIndex == -1)
/* 221*/            return;
/* 223*/        IRow prow = tblMain.getRow(rowIndex);
/* 224*/        String id = prow.getCell("id").getValue().toString();
/* 225*/        MarketPlanInfo EnterInfo = MarketPlanFactory.getRemoteInstance().getMarketPlanInfo(new ObjectUuidPK(id.toString()), getEntrySelectors());
/* 226*/        MarketPlanEntryCollection enterEntryColl = EnterInfo.getEntry();
/* 227*/        CRMHelper.sortCollection(enterEntryColl, "seq", true);
/* 228*/        for(int i = 0; i < enterEntryColl.size(); i++)
        {
/* 229*/            MarketPlanEntryInfo planEntryInfo = enterEntryColl.get(i);
/* 231*/            MarketPlanEntrySellPlanEntryCollection sellCol = planEntryInfo.getSellPlanEntry();
/* 232*/            CRMHelper.sortCollection(sellCol, "seq", true);
/* 234*/            for(int j = 0; j < sellCol.size(); j++)
            {
/* 236*/                MarketPlanEntrySellPlanEntryInfo sellInfo = sellCol.get(j);
/* 238*/                IRow row = tblMain2.addRow();
/* 239*/                row.getCell("id").setValue(planEntryInfo.getId());
/* 240*/                row.getCell("themeState").setValue(planEntryInfo.getState());
/* 241*/                row.getCell("theme").setValue(planEntryInfo.getTheme());
/* 242*/                row.getCell("enDescribe").setValue(planEntryInfo.getDescribe());
/* 244*/                row.getCell("proceeding").setValue(sellInfo.getProceeding());
/* 245*/                row.getCell("entryState").setValue(sellInfo.getState());
/* 246*/                row.getCell("subject").setValue(sellInfo.getSubject());
/* 247*/                row.getCell("classify").setValue(sellInfo.getClassify());
/* 248*/                row.getCell("mediaName").setValue(sellInfo.getMediaName());
/* 249*/                row.getCell("startTime").setValue(sellInfo.getStartTime());
/* 250*/                row.getCell("endTime").setValue(sellInfo.getEndTime());
/* 251*/                row.getCell("quantity").setValue(Integer.valueOf(sellInfo.getQuantity()));
/* 252*/                row.getCell("planCost").setValue(sellInfo.getPlanCost());
/* 253*/                row.getCell("remark").setValue(sellInfo.getRemark());
            }

        }

/* 256*/        MarketPlanEditUI.mergerTable(tblMain2, new String[] {
/* 256*/            "id"
        }, new String[] {
/* 256*/            "theme", "themeState", "enDescribe"
        });
    }

    public SelectorItemCollection getPalnThemeSelectors()
    {
/* 260*/        SelectorItemCollection sel = super.getSelectors();
/* 261*/        sel.add("*");
/* 262*/        sel.add("subject.*");
/* 263*/        sel.add("classify.*");
/* 264*/        sel.add("mediaName.*");
/* 265*/        sel.add("CU.*");
/* 266*/        return sel;
    }

    protected void tblMain_tableSelectChanged(KDTSelectEvent e)
        throws Exception
    {
/* 270*/        updateEnterprisePlanEntry();
/* 271*/        MeasurePlanTargetListUI.setBtnState(tblMain, getBizInterface(), actionAudit, actionUnAudit, actionEdit, actionRemove);
    }

    protected boolean isIgnoreCUFilter()
    {
/* 274*/        return true;
    }

    public boolean isIgnoreRowCount()
    {
/* 277*/        return false;
    }

    protected void prepareUIContext(UIContext uiContext, ActionEvent e)
    {
/* 283*/        super.prepareUIContext(uiContext, e);
/* 284*/        com.kingdee.bos.ui.face.ItemAction act = getActionFromActionEvent(e);
/* 285*/        if(act.equals(actionAddNew))
/* 286*/            if(getSelectedTreeNode() != null)
            {
/* 287*/                Object userObject2 = getSelectedTreeNode().getUserObject();
/* 288*/                if(userObject2 instanceof SellProjectInfo)
                {
/* 289*/                    BOSUuid sellProjectId = ((SellProjectInfo)userObject2).getId();
/* 290*/                    uiContext.put("sellProjectId", sellProjectId.toString());
                } else
                {
/* 292*/                    MsgBox.showWarning("\u8BF7\u9009\u4E2D\u5177\u4F53\u9500\u552E\u9879\u76EE\uFF01");
/* 293*/                    SysUtil.abort();
                }
            } else
            {
/* 296*/                MsgBox.showWarning("\u8BF7\u9009\u4E2D\u5177\u4F53\u9500\u552E\u9879\u76EE\uFF01");
/* 297*/                SysUtil.abort();
            }
    }

    protected IQueryExecutor getQueryExecutor(IMetaDataPK pk, EntityViewInfo viewInfo)
    {
/* 306*/        EntityViewInfo view = (EntityViewInfo)viewInfo.clone();
/* 308*/        DefaultKingdeeTreeNode node = (DefaultKingdeeTreeNode)treeMain.getLastSelectedPathComponent();
/* 309*/        FilterInfo filter = new FilterInfo();
/* 310*/        if(node != null)
/* 311*/            if(node.getUserObject() instanceof SellProjectInfo)
            {
/* 312*/                SellProjectInfo info = (SellProjectInfo)node.getUserObject();
/* 313*/                filter.getFilterItems().add(new FilterItemInfo("sellProject.id", info.getId().toString()));
            } else
/* 314*/            if(node.getUserObject() instanceof OrgStructureInfo)
/* 315*/                filter.getFilterItems().add(new FilterItemInfo("sellProject.id", MeasurePlanTargetListUI.getAllProjectNodeId(node), CompareType.INCLUDE));
/* 319*/        try
        {
/* 319*/            if(view.getFilter() != null)
/* 320*/                view.getFilter().mergeFilter(filter, "and");
/* 322*/            else
/* 322*/                view.setFilter(filter);
/* 325*/            if(view.getSorter().size() < 2)
            {
/* 326*/                SorterItemCollection sort = new SorterItemCollection();
/* 327*/                SorterItemInfo planYear = new SorterItemInfo("planYear");
/* 328*/                planYear.setSortType(SortType.DESCEND);
/* 329*/                sort.add(planYear);
/* 331*/                SorterItemInfo planMonth = new SorterItemInfo("planMonth");
/* 332*/                planMonth.setSortType(SortType.DESCEND);
/* 333*/                sort.add(planMonth);
/* 335*/                SorterItemInfo version = new SorterItemInfo("version");
/* 336*/                version.setSortType(SortType.DESCEND);
/* 337*/                sort.add(version);
/* 339*/                view.setSorter(sort);
            }
        }
/* 341*/        catch(BOSException e)
        {
/* 342*/            e.printStackTrace();
        }
/* 344*/        return super.getQueryExecutor(pk, view);
    }

    protected void tblMain2_tableClicked(KDTMouseEvent e)
        throws Exception
    {
/* 347*/        super.tblMain2_tableClicked(e);
/* 348*/        if(e.getColIndex() == tblMain2.getColumnIndex("enDescribe"))
/* 350*/            showDialog(this, tblMain2, 250, 100, 2000);
    }

    public void showDialog(JComponent owner, KDTable table, int X, int Y, int len)
    {
/* 356*/        int rowIndex = table.getSelectManager().getActiveRowIndex();
/* 357*/        int colIndex = table.getSelectManager().getActiveColumnIndex();
/* 358*/        ICell cell = table.getCell(rowIndex, colIndex);
/* 359*/        if(cell.getValue() == null)
/* 360*/            return;
/* 361*/        BasicView view = table.getViewManager().getView(5);
/* 362*/        Point p = view.getLocationOnScreen();
/* 363*/        Rectangle rect = view.getCellRectangle(rowIndex, colIndex);
/* 364*/        java.awt.Window parent = SwingUtilities.getWindowAncestor(owner);
        KDDetailedAreaDialog dialog;
/* 366*/        if(parent instanceof Dialog)
/* 368*/            dialog = new KDDetailedAreaDialog((Dialog)parent, X, Y, true);
/* 371*/        else
/* 371*/        if(parent instanceof Frame)
/* 372*/            dialog = new KDDetailedAreaDialog((Frame)parent, X, Y, true);
/* 375*/        else
/* 375*/            dialog = new KDDetailedAreaDialog(true);
/* 377*/        String vals = cell.getValue().toString();
/* 378*/        dialog.setData(vals);
/* 379*/        dialog.setPRENTE_X(p.x + rect.x + rect.width);
/* 380*/        dialog.setPRENTE_Y(p.y + rect.y);
/* 381*/        dialog.setMaxLength(len);
/* 382*/        dialog.setEditable(false);
/* 383*/        dialog.show();
    }

    /**
     * output getKeyFieldName method
     */
    protected String getKeyFieldName()
    {
        return "id";
    }

    /**
     * output createNewData method
     */
    protected com.kingdee.bos.dao.IObjectValue createNewData()
    {
        com.kingdee.eas.crm.market.MarketPlanInfo objectValue = new com.kingdee.eas.crm.market.MarketPlanInfo();
		
        return objectValue;
    }

}